package com.qs.eggs.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.account.LoginActivity;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.widgets.custom.FullyGridLayoutManager;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;


public class ProAdapter extends RecyclerView.Adapter<ProAdapter.ViewHolder>  implements View.OnClickListener{
    private LayoutInflater mInflater;
    private List<Map<String, Object>> giflist=new ArrayList<>();
    private Context context;
    private  String imageUrl;

    public ProAdapter(Context context, List<Map<String, Object>> giflist) {
        mInflater = LayoutInflater.from(context);
        this.giflist = giflist;
        this.context=context;
    }
    private OnItemClickListener mOnItemClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        TextView name,price;
        ImageView image;
    }

    @Override
    public int getItemCount()
    {
        return giflist.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
    {
        View view = mInflater.inflate(R.layout.buy,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        viewHolder.name= (TextView) view.findViewById(R.id.name);
        viewHolder.price= (TextView) view.findViewById(R.id.jiage);
        viewHolder.image = (ImageView) view.findViewById(R.id.imageView);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(ViewHolder viewHolder,final int position) {
        viewHolder.itemView.setTag(position);
        imageUrl= Constants.URL_BASE+"productimages/"+giflist.get(position).get("autoname");
        Picasso.with(context).load(imageUrl).into(viewHolder.image);
        viewHolder.name.setText(giflist.get(position).get("proname").toString());
        viewHolder.price.setText("¥"+giflist.get(position).get("price")+"");
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
